/**
 * Copyright (c) 2009, Jacek Karczmarczyk
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * - Neither the name of the author nor the names of its contributors may be
 *   used to endorse or promote products derived from this software without
 *   specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 *
 * @package Larchana_Javascript
 * @version 0.1
 * @author Jacek Karczmarczyk <jacek@karczmarczyk.pl>
 */

/**
 * Default handler for ajax operations
 *
 *
 * Example usage:
 * $.ajax({
 *   complete: $.ajaxComplete({
 *     success: function (message) {
 *       alert("OK");
 *     }, 
 *     error: function (message) {
 *       alert("Some error: " + message);
 *     },
 *     complete: function() {
 *       alert("Request completed");
 *     }
 *   })
 * });
 *
 * Or (error message will be displayed automaticly):
 *
 * $.ajax({
 *   complete: $.ajaxComplete(function (message) {
 *     alert("OK");
 *   })
 * });
 *
 * Every message sent from the server must be in JSON format:
 * { type: "error"; message: "user data, eg. message to display" }
 * { type: "success"; message: "user data, eg. message to display, or some html, etc" }
 * If the message is not in JSON format or doesn't contain the "type" field or the value
 * of the "type" field is no "error" or "success" the error handler is triggered
 *
 */

"use strict";

(function ($, window) {

	$.ajaxComplete = function (handlers) {
		return function (XMLHttpRequest, textStatus) {
			var response;
			if (XMLHttpRequest.statusText !== 'OK') {
				window.document.location = window.document.location;
				return;
			}

			if (handlers === undefined) {
				handlers = {};
			}
			if (typeof handlers === 'function') {
				handlers = {success: handlers};
			}
			if (handlers.error === undefined) {
				handlers.error = function (message) { 
					window.alert(message);
				};
			}

			try { 
				response = JSON.parse(XMLHttpRequest.responseText);
			}
			catch (e) {
				response = {type: 'error', message: 'XMLHttpRequest failed: response parse error'};
			}
			if (response.type !== 'success' && response.type !== 'error') {
				response = {type: 'error', message: 'XMLHttpRequest failed: invalid response type'};
			}

			if (typeof handlers[response.type] === 'function') {
				handlers[response.type](response.message);
			}
			if (typeof handlers.complete === 'function') {
				handlers.complete();
			}
		};
	};
}(jQuery, window));